package milu.models;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Embedded;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Text;

import com.google.appengine.api.users.*;


import milu.*;


@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Recipe {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private Date created;
    @Persistent
    private Key userKey; // user that created this recipe
    @Persistent
    private String title;
    @Persistent
    private Text content;
    @Persistent
    private Text ingredients;
    @Persistent
    private Key fullImageKey;
    @Persistent
    private Key thumbKey;
    
    @Persistent
    private Set<String> tags; // the actual tags

    public Recipe() {
        this.created = new Date();
    }
    public Recipe(Key userKey, String title, Text content, Text ingredients, Set<String> tags) {
        this.created = new Date();
        this.userKey = userKey;
        this.title = title;
        this.content = content;
        this.ingredients = ingredients;
        this.tags = tags;
    }
    public Key getKey() {
        return key;
    }
    public Key getUserKey() {
        return userKey;
    }
    public void setUserKey(Key userKey) {
        this.userKey = userKey;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Text getContent() {
        return content;
    }
    public void setContent(Text content) {
        this.content = content;
    }
    public Text getIngredients() {
        return ingredients;
    }
    public void setIngredients(Text ingredients) {
        this.ingredients = ingredients;
    }
    public Key getFullImageKey() {
        return fullImageKey;
    }
    public Key getThumbKey() {
        return thumbKey;
    }
    public void setFullImageKey(Key fullImageKey) {
        this.fullImageKey = fullImageKey;
    }
    public void setThumbKey(Key thumbKey) {
        this.thumbKey = thumbKey;
    }
    public Set<String> getTags() {
        return tags;
    }
    public void setTags(Set<String> tags) {
        this.tags = tags;
    }
    
    public MyImage getMyImage() {
        PersistenceManager pm = Datastore.getPersistenceManager();
        MyImage myImage = pm.getObjectById(MyImage.class, this.fullImageKey);
        return myImage;
    }
    public MyImage getThumb() {
        PersistenceManager pm = Datastore.getPersistenceManager();
        MyImage myImage = pm.getObjectById(MyImage.class, this.thumbKey);
        return myImage;
    }

    public static List<Recipe> getRecipes(Key userKey) {
        PersistenceManager pm = Datastore.getPersistenceManager();

        Query query = pm.newQuery(Recipe.class, "this.userKey == userKey");
        query.setOrdering("created desc");
        
        query.declareImports("import com.google.appengine.api.datastore.Key");
        query.declareParameters("Key userKey");
        List<Recipe> recipes = (List<Recipe>)query.execute(userKey);

        return recipes;
    }
}
